document.write("<script src=\"" + context
		+ "/system/user/js/editpwd.js\"></script>");

Ext.onReady(function() {

	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';

	// This is an inner body element within the Details panel created to provide
	// a "slide in" effect
	// on the panel body without affecting the body's box itself. This element
	// is created on
	// initial use and cached in this var for subsequent access.
	var detailEl;

	// This is the main content center region that will contain each example
	// layout panel.
	// It will be implemented as a CardLayout since it will contain multiple
	// panels with
	// only one being visible at any given time.
	var contentPanel = {
		id : 'content-panel',
		region : 'center', // this is what makes this panel into a region
		// within the containing layout
		layout : 'card',
		margins : '2 5 5 0',
		activeItem : 0,
		border : false,
		items : []
	};

	var tabs = new Ext.TabPanel({
				id : 'consoletabs',
				region : 'center', // a center region is ALWAYS
				// required for border layout
				closable : true,
				resizeTabs : true,
				minTabWidth : 150,
				tabWidth : 100,
				border : false,
				deferredRender : false,
				enableTabScroll : true,
				margins : '5 5 5 0',
				activeTab : 0, // first tab initially active
				items : []
			});

	tabs.add({
				id : 'desktop',
				title : '我的桌面',
				tabTip : '个人桌面',
				tabWidth : 100,
				iconCls : 'desktop',
				closable : false
			}).show();

	// Go ahead and create the TreePanel now so that we can use it below
	var treePanel = new Ext.tree.TreePanel({
		id : 'tree-panel',
		split : true,
		layout : 'fit',
		autoScroll : true,
		border : false,
		title : '应用模块菜单',
		// tree-specific configs:
		rootVisible : false,
		lines : false,
		singleExpand : false,
		useArrows : true,
		dataUrl : context + '/system/module/menutreejson.jsp',

		root : new Ext.tree.AsyncTreeNode(),
		listeners : {
			'click' : function(node) {
				if (node && node.leaf) {
					if (node.attributes.mnblank == 1) {
						Ext.get('menuhref').dom.href = context
								+ node.attributes.mnurl;
						Ext.get('menuhref').dom.target = '_blank';
						Ext.get('menuhref').dom.click();
						// showWindow(node.attributes.mnurl, 800, 600);
					} else {
						var tabid = 'tab_' + node.attributes.realid;

						var url = node.attributes.mnurl;

						if (url.indexOf('javascript:') > -1) {
							var func = url.substring(url.indexOf('javascript:')
									+ 11);

							var m = null;

							if (func.indexOf('?') > -1) {
								m = urlToObject(func);

								func = func.substring(0, func.indexOf('?'));
							}

							if (func == null || func == '') {
								Ext.Msg.alert('提示', '菜单<' + node.text
												+ '>链接<pre>'
												+ node.attributes.mnurl
												+ '</pre>定义有误！');
								return;
							}

							if (!Ext.getCmp(tabid)) {
								showMenuOnTab(tabid, node.attributes.realid,
										node.text, func, m);

							} else {
								Ext.getCmp('consoletabs').setActiveTab(tabid);
								/*
								 * Ext.get(tabid + '_frm').dom.src = context +
								 * node.attributes.mnurl;
								 */
							}
						} else {
							var tabid = 'tab_' + node.attributes.realid;

							if (!Ext.getCmp(tabid)) {
								Ext.getCmp('consoletabs').add({
									id : tabid,
									title : node.text,
									tabTip : node.attributes.mnmemo == ''
											? (node.parentNode.text + '/' + node.text)
											: node.attributes.mnmemo,
									iconCls : 'tabs',
									html : '<iframe src="'
											+ context
											+ node.attributes.mnurl
											+ '" id="'
											+ tabid
											+ '_frm" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>',
									closable : true
								}).show();
							} else {
								Ext.getCmp('consoletabs').setActiveTab(tabid);
								/*
								 * Ext.get(tabid + '_frm').dom.src = context +
								 * node.attributes.mnurl;
								 */
							}
						}

					}
				} else if (node && !node.leaf && !node.isExpanded()) {
					node.expand(true);
				} else if (node && !node.leaf && node.isExpanded()) {
					node.collapse(true);
				}
			}

		}
	});

	treePanel.expandAll();

	var tbarItems = [{
				iconCls : 'pwd',
				tooltip : '可进行个人密码修改设置',
				handler : function() {
					editPWD();
				},
				text : '密码管理',
				scope : this
			}, {
				iconCls : 'refresh',
				tooltip : '刷新工作区页面',
				handler : function() {
					refre();
				},
				text : '刷新选项卡',
				scope : this
			}];

	if (roles.containsKey('admin')) {
		tbarItems.push({
					iconCls : 'console',
					tooltip : '应用支撑平台',
					handler : function() {
						Ext.get('menuhref').dom.href = context
								+ '/system/console';
						Ext.get('menuhref').dom.target = '_blank';
						Ext.get('menuhref').dom.click();
					},
					text : '控制台',
					scope : this
				});
	}

	tbarItems.push({
				iconCls : 'logout',
				tooltip : '注销当前会话,退出系统',
				handler : function() {
					logout();
				},
				text : '退出系统',
				scope : this
			});

	var viewport = new Ext.Viewport({
				layout : 'border',

				items : [{
							xtype : 'box',
							region : 'north',
							applyTo : 'header',
							height : 50
						}, {
							id : 'btns',
							region : 'west',
							baseCls : 'x-plain',
							split : true,
							animCollapse : true,
							// animate: false,
							collapseMode : 'mini',
							width : 250,
							minWidth : 200,
							maxWidth : 300,
							layout : 'fit',
							margins : '5 0 5 5',
							tbar : tbarItems,
							items : [treePanel]
						}, tabs]
			});
	$('userNameSpan').innerHTML = '<span class="left">会话用户_</span> <span class="right">'
			+ userName + '</span>';
	$('groupNameSpan').innerHTML = '<span class="left">所在部门_</span> <span class="right">'
			+ groupName + '</span>';

	setTimeout(function() {
				Ext.get('loading').remove();
				Ext.get('loading-mask').fadeOut({
							remove : true
						});
			}, 250);
			
	inithelper();		
});

/* 退出系统 */
function logout() {
	Ext.MessageBox.confirm('确认', '确定退出系统？', function(e) {
				if (e == "yes") {
					document.location = context + '/system/logout.do';
				}
			});

}

function refre() {
	var tab = Ext.getCmp('consoletabs').getActiveTab();
	var frm = Ext.get(tab.id + '_frm');
	if (frm != null) {
		frm.dom.src = frm.dom.src;
	}
}

/* tab 页中显示 */
// tabid
// id is menuId
// tlt is menuName
function showMenuOnTab(tabid, id, tlt, func, m) {
	if (!Ext.getCmp(tabid)) {
		var options = null;

		if (m != null) {
			options = m;
			options['tabid'] = tabid;
			options['tabcontainerid'] = 'consoletabs';
			options['title'] = tlt;
			options['frozen'] = true
		} else {
			options = {
				'tabid' : tabid,
				'tabcontainerid' : 'consoletabs',
				'title' : tlt,
				'frozen' : true
			};
		}

		eval('var tabObj = ' + func + '(' + Ext.util.JSON.encode(options)
				+ ');');
	}

	Ext.getCmp('consoletabs').setActiveTab(tabid);
}

/**
 * load wordbook menu
 * 
 * @param {}
 *            options
 */
function wordbookMenu(options) {
	var tabObj = initWordbookGrid(options);

	tabObj.setIconClass('tabs');

	Ext.getCmp(options.tabcontainerid).add(tabObj);
}

/**
 * load table query menu
 * 
 * @param {}
 *            options
 */
function tablequeryMenu(options) {
	initTQExtTabList(options);
}

/**
 * load table view menu
 * 
 * @param {}
 *            options
 */
function tableviewMenu(options) {
	initTableViewTab(options);
}